<?php
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use app\admin\model\#MODELNAME#;
use app\admin\model\ArticleCategoryModel;
use think\Db;

class #CONTROLLERNAME# extends AdminBaseController
{
    /**
     * #DESC#列表
     */
    public function index()
    {
		$param = $this->request->param();
		$model = new #MODELNAME#();
		
		#GET_LIST#
		
        return $this->fetch();
    }

    /**
     * 添加#DESC#
     */
    public function add()
    {
		#ADD_TODO#
        return $this->fetch();
    }

    /**
     * 添加#DESC#提交
     */
    public function addPost()
    {
        if ($this->request->isPost()) {
			$data  =  $this->request->param();
			$model = new #MODELNAME#();
			$result = $this->validate($data, '#VALIDATENAME#');
			 if ($result !== true) {
				$this->error($result);
			}
			
			#ADDPOST#
			
			$rest = $model->insert($data);

			if ($rest === false) {
				$this->error('添加失败!');
			}
			$this->success('添加成功!',url('index'));
		}

    }

    /**
     * 编辑#DESC#
     */
    public function edit()
    {
		$id = $this->request->param('#ID#');
		$model = new #MODELNAME#();
		$info = $model->find($id);
		#EDIT_TODO#
		//编辑信息
		$this->assign('post',$info);
        return $this->fetch();
    }

    /**
     * 编辑#DESC#提交
     */
    public function editPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
			$model = new #MODELNAME#();
			$result = $this->validate($data, '#VALIDATENAME#');

			if ($result !== true) {
				$this->error($result);
			}
			
			#EDITPOST#
			
			$rest = $model->where("id",$data['id'])->update($data);

			if ($rest === false) {
				$this->error('修改失败!');
			}
			$this->success('修改成功!',url('index'));
        }
    }

    /**
     * #DESC#删除
     */
    public function delete()
    {
        $model = new #MODELNAME#();
		$id = $this->request->param('#ID#');
		
		
		$res = $model->where("id",$id)->delete();
		if($res){
			$this->success('删除成功!');
		}else{
			$this->error('删除失败');
		}
    }
	
	/**
     * #DESC#排序
     */
    public function listOrder()
    {
        $data = $this->request->param();
        $model = new #MODELNAME#();
        if(isset($data['listorders'])){
            foreach ($data['listorders'] as $k=>$v){
                $model->where(['id' => $k])->update(['#listorder#' => $v]);
            }
        }
        $this->success("排序更新成功！", '');
    }

    /**
     * #DESC#显示和隐藏
     */
    public function toggle()
    {
        $data = $this->request->param();
        $model = new #MODELNAME#();

        if (isset($data['ids']) && !empty($data["display"])) {
            $ids = $this->request->param('ids/a');
            $model->where(['id' => ['in', $ids]])->update(['#status#' => 1]);
            $this->success("更新成功！");
        }

        if (isset($data['ids']) && !empty($data["hide"])) {
            $ids = $this->request->param('ids/a');
            $model->where(['id' => ['in', $ids]])->update(['#status#' => 0]);
            $this->success("更新成功！");
        }

    }

}
